package hk.edu.polyu.comp446.group5;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import org.jfree.ui.RefineryUtilities;

; //Added by Joe Tam for holding the stocks selected by JCheckboxes 

public class StockMain extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTabbedPane tabbedpane;
	private JPanel contentPane;
	private JPanel stockPane;
	private JPanel buttonPane;

	private JPanel secmainpanel;  // JPanel for holding portsumpanel and secrightpanel
	private JPanel portsumpanel;  // JPanel for holding secleftpanel and secrightpanel
	private JPanel secleftpanel;  // JPanel for holding and stock slection and Portfolio summary
	private JPanel secrightpanel;  // JPanel for holding assetalloc and effport
	private JPanel assetalloc;  // JPanel of Asset allocation (subpanel);
	private JPanel effport;  // JPanel for efficent portfolio (subpanel);
	private JButton startApp;
	private JButton exitApp;

	// String[][] tabledata = new String[5][];

	private String[] columnNames = { "Stock Code", "Expected Return", "Standard Deviation", "Variance", "Skewness", "Excess Kurtosis", "Value-at-Risk", "Sharpe Ratio" };
	private String[] resultcolumns = { "Global Minimum", "Tangency Portfolio" };

	private JTextField singlestock = new JTextField(10);

	// JTextField and JLabel for asset allocation
	private JTextField stockA = new JTextField(10);
	private JTextField stockB = new JTextField(10);
	private JLabel selectstockA = new JLabel("Not Selected");
	private JLabel selectstockB = new JLabel("Not Selected");

	// JTextFields for Portfolio summary
	private JTextField promean = new JTextField(10);
	private JTextField provariance = new JTextField(10);
	private JTextField prosd = new JTextField(10);
	private JTextField provarisk = new JTextField(10);
	private JTextField prosharpe = new JTextField(10);
	private JTextField initwealth = new JTextField(10);
	private JTextField proquantile = new JTextField(10);

	// JTextFields for portfolio analysis
	private JTextField porttgtexp = new JTextField(10);
	private JTextField porttgtrisk = new JTextField(10);
	// private JTextField provar = new JTextField(10);

	// JTextFields for Expected return and risk
	// private JTextField exprtn = new JTextField(10);
	// private JTextField exprisk = new JTextField(10);

	private JCheckBox stock1 = new JCheckBox("");
	private JCheckBox stock2 = new JCheckBox("");
	private JCheckBox stock3 = new JCheckBox("");
	private JCheckBox stock4 = new JCheckBox("");
	private JCheckBox stock5 = new JCheckBox("");
	private JCheckBox stock6 = new JCheckBox("");

	private JButton showstock1 = new JButton("Price Chart");
	private JButton showstock2 = new JButton("Price Chart");
	private JButton showstock3 = new JButton("Price Chart");
	private JButton showstock4 = new JButton("Price Chart");
	private JButton showstock5 = new JButton("Price Chart");
	private JButton showstock6 = new JButton("Price Chart");

	private JButton showccstock1 = new JButton("CC Return Chart");
	private JButton showccstock2 = new JButton("CC Return Chart");
	private JButton showccstock3 = new JButton("CC Return Chart");
	private JButton showccstock4 = new JButton("CC Return Chart");
	private JButton showccstock5 = new JButton("CC Return Chart");
	private JButton showccstock6 = new JButton("CC Return Chart");

	private JButton checkstock;
	private JButton showallstock;
	private JButton saveresult;

	// For getting portfolio summary
	private JButton getportresult = new JButton("Update");

	private JButton updateassetalloc = new JButton("Update");
	private JButton genglobalminpro = new JButton("Generate");
	private JButton gentanpro = new JButton("Generate");
	private JButton gentanexprtn = new JButton("Update");
	private JButton gentgtriskrtn = new JButton("Update");
	private JButton reportassetalloc = new JButton("Reset");

	// Add the table for display stock data
	private DefaultTableModel tableModel;

	private ArrayList<String> stockcombination = new ArrayList<String>();
	private ArrayList<String> logger = new ArrayList<String>();

	int selectedStock = 0;
	double allocatedasset = 0.0;
	double allocatedstockA = 0.0;
	double allocatedstockB = 0.0;
	double allocatedinitwealth = 0.0;
	double allocatedinitquan = 0.0;
	double allocatedexprtn = 0.0;
	double allocatedexprisk = 0.0;

	char alloccontrolbit = 'F'; // Check if the allocation of A and B is 100
	char stockcontrolbit = 'F'; // Check if the users has selected 2 stocks
	char initweathcontrolbit = 'F';
	char quancontrolbit = 'F';

	private SimpleDateFormat dateAsText = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // For writing to report

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// SimpleDateFormat dateAsText = new
		// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// StockBean stock =
		// StockTickerDAO.getInstance().getStockPrice("vpacx");
		StockMain frame = new StockMain();
		frame.setVisible(true);

		// Added by Joe Tam
	}

	public StockMain() {

		this.logger.add("Time: " + this.dateAsText.format(new Date()) + " Program start");
		this.initwealth.setText("1.0");
		this.proquantile.setText("0.05");
		// this.exprtn.setText("0.008");
		// this.exprisk.setText("0.6");
		tabbedpane = new JTabbedPane();

		this.startApp = new JButton("Start");
		this.exitApp = new JButton("Exit");
		this.checkstock = new JButton("Show data");
		this.showallstock = new JButton("Show All");
		this.saveresult = new JButton("Save result");

		this.startApp.addActionListener(this);
		this.exitApp.addActionListener(this);
		this.checkstock.addActionListener(this);
		this.showallstock.addActionListener(this);
		this.saveresult.addActionListener(this);

		this.genglobalminpro.addActionListener(this);
		this.gentanpro.addActionListener(this);
		this.gentanexprtn.addActionListener(this);
		this.gentgtriskrtn.addActionListener(this);
		this.reportassetalloc.addActionListener(this);

		this.stock1.addActionListener(this);
		this.stock2.addActionListener(this);
		this.stock3.addActionListener(this);
		this.stock4.addActionListener(this);
		this.stock5.addActionListener(this);
		this.stock6.addActionListener(this);

		this.showstock1.addActionListener(this);
		this.showstock2.addActionListener(this);
		this.showstock3.addActionListener(this);
		this.showstock4.addActionListener(this);
		this.showstock5.addActionListener(this);
		this.showstock6.addActionListener(this);

		this.showccstock1.addActionListener(this);
		this.showccstock2.addActionListener(this);
		this.showccstock3.addActionListener(this);
		this.showccstock4.addActionListener(this);
		this.showccstock5.addActionListener(this);
		this.showccstock6.addActionListener(this);

		this.promean.setEditable(false);
		this.provariance.setEditable(false);
		this.prosd.setEditable(false);
		this.provarisk.setEditable(false);
		this.prosharpe.setEditable(false);

		this.updateassetalloc.addActionListener(this);

		this.stockA.setText("0.5");
		this.stockB.setText("0.5");

		GridBagLayout tpanellayout = new GridBagLayout();
		GridBagConstraints tpanelgbc = new GridBagConstraints();

		GridBagLayout stockpanellayout = new GridBagLayout();
		GridBagConstraints spanelgbc = new GridBagConstraints();

		GridBagLayout buttonlayout = new GridBagLayout();
		GridBagConstraints buttongbc = new GridBagConstraints();

		// For setting constraint on portfolio summary
		GridBagLayout portsumlayout = new GridBagLayout();
		GridBagConstraints portsumgbc = new GridBagConstraints();

		// For second tab
		GridBagLayout assetlayout = new GridBagLayout();
		GridBagConstraints assetpanelgbc = new GridBagConstraints();

		GridBagLayout eflayout = new GridBagLayout();
		GridBagConstraints efpanelgbc = new GridBagConstraints();

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1200, 880);
		setTitle("COMP446 Computational Finance System");
		contentPane = new JPanel();
		contentPane.setLayout(tpanellayout);

		LineBorder stockinborder = new LineBorder(Color.BLACK, 1);
		TitledBorder stockborder = new TitledBorder(stockinborder, "Please Select 2 Stocks", TitledBorder.CENTER, TitledBorder.TOP);
		stockPane = new JPanel();
		stockPane.setBorder(stockborder);
		stockPane.setLayout(stockpanellayout);

		buttonPane = new JPanel();
		buttonPane.setLayout(buttonlayout);

		secmainpanel = new JPanel();
		secmainpanel.setLayout(new GridLayout(1, 2));

		secleftpanel = new JPanel();
		secleftpanel.setLayout(new GridLayout(2, 1));

		secrightpanel = new JPanel();
		secrightpanel.setLayout(new GridLayout(2, 1));

		LineBorder portsuminborder = new LineBorder(Color.BLACK, 1);
		TitledBorder portsumborder = new TitledBorder(portsuminborder, "Portfolio Summary", TitledBorder.CENTER, TitledBorder.TOP);
		portsumpanel = new JPanel();
		portsumpanel.setLayout(portsumlayout);
		portsumpanel.setBorder(portsumborder);

		LineBorder assetinborder = new LineBorder(Color.BLACK, 1);
		TitledBorder assetborder = new TitledBorder(assetinborder, "Asset Allocation", TitledBorder.CENTER, TitledBorder.TOP);
		assetalloc = new JPanel();
		assetalloc.setLayout(assetlayout);
		assetalloc.setBorder(assetborder);

		LineBorder effinborder = new LineBorder(Color.black, 1);
		TitledBorder effborder = new TitledBorder(effinborder, "Efficient Portfolios", TitledBorder.CENTER, TitledBorder.TOP);
		effport = new JPanel();
		effport.setLayout(eflayout);
		effport.setBorder(effborder);

		tableModel = new DefaultTableModel(columnNames, 0);
		JTable table = new JTable(tableModel);
		table.setAutoCreateRowSorter(true);
		table.setDefaultEditor(Object.class, null);
		JScrollPane sp = new JScrollPane(table);
		sp.setPreferredSize(new Dimension(1000, 120));

		// JTable for final result
		DefaultTableModel resulttable = new DefaultTableModel(resultcolumns, 0);
		JTable table2 = new JTable(resulttable);
		table2.setAutoCreateRowSorter(true);
		JScrollPane sp2 = new JScrollPane(table2);
		sp2.setPreferredSize(new Dimension(950, 200));

		tpanelgbc.gridx = 0;
		tpanelgbc.gridy = 0;
		tpanelgbc.gridwidth = 10;
		tpanelgbc.insets = new Insets(5, 10, 5, 10);
		contentPane.add(new JLabel("Display Stocks Data"), tpanelgbc);

		tpanelgbc.gridy = 1;
		tpanelgbc.gridx = 6;
		tpanelgbc.gridwidth = 1;
		// tpanelgbc.weightx = 2.0d;
		tpanelgbc.insets = new Insets(5, 400, 5, 10);
		contentPane.add(new JLabel("Enter Stock Code: "), tpanelgbc);

		tpanelgbc.gridy = 1;
		tpanelgbc.gridx = 7;
		tpanelgbc.gridwidth = 1;
		// tpanelgbc.weightx = 0.0d;
		tpanelgbc.insets = new Insets(5, 0, 5, 10);
		contentPane.add(this.singlestock, tpanelgbc);

		tpanelgbc.gridy = 1;
		tpanelgbc.gridx = 8;
		tpanelgbc.gridwidth = 1;
		tpanelgbc.insets = new Insets(5, 0, 5, 10);
		contentPane.add(this.checkstock, tpanelgbc);

		tpanelgbc.gridy = 1;
		tpanelgbc.gridx = 9;
		tpanelgbc.gridwidth = 1;
		tpanelgbc.insets = new Insets(5, -150, 5, 10);
		contentPane.add(this.showallstock, tpanelgbc);

		tpanelgbc.gridy = 1;
		tpanelgbc.gridx = 10;
		tpanelgbc.gridwidth = 1;
		tpanelgbc.insets = new Insets(5, -190, 5, 10);
		contentPane.add(this.saveresult, tpanelgbc);

		tpanelgbc.gridx = 0;
		tpanelgbc.gridy = 2;
		tpanelgbc.gridwidth = 10;
		tpanelgbc.weightx = 0.0;
		tpanelgbc.insets = new Insets(5, 10, 5, 10);
		contentPane.add(sp, tpanelgbc);

		// Add elements for selecting stocks and show charts
		// spanelgbc.gridy = 0;
		// spanelgbc.insets = new Insets(5, 0, 5, 10);
		// spanelgbc.gridwidth = 4;
		// stockPane.add(new
		// JLabel("You can view information by clicking Show Chart and CC return button"),
		// spanelgbc);

		spanelgbc.anchor = GridBagConstraints.EAST;
		spanelgbc.gridy = 0;
		spanelgbc.insets = new Insets(5, -10, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(new JLabel("S&P 500 index: vfinx"), spanelgbc);

		spanelgbc.gridy = 0;
		spanelgbc.gridx = 1;
		spanelgbc.insets = new Insets(5, 70, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(this.stock1, spanelgbc);

		spanelgbc.gridy = 0;
		spanelgbc.gridx = 2;
		spanelgbc.insets = new Insets(5, -10, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(this.showstock1, spanelgbc);

		spanelgbc.gridy = 0;
		spanelgbc.gridx = 3;
		spanelgbc.insets = new Insets(5, -10, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(this.showccstock1, spanelgbc);

		spanelgbc.gridy = 1;
		spanelgbc.gridx = 0;
		spanelgbc.insets = new Insets(5, 30, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(new JLabel("European stock index: veurx"), spanelgbc);

		spanelgbc.gridy = 1;
		spanelgbc.gridx = 1;
		spanelgbc.insets = new Insets(5, 70, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(this.stock2, spanelgbc);

		spanelgbc.gridy = 1;
		spanelgbc.gridx = 2;
		spanelgbc.insets = new Insets(5, -10, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(this.showstock2, spanelgbc);

		spanelgbc.gridy = 1;
		spanelgbc.gridx = 3;
		spanelgbc.insets = new Insets(5, -10, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(this.showccstock2, spanelgbc);

		spanelgbc.gridy = 3;
		spanelgbc.gridx = 0;
		spanelgbc.insets = new Insets(5, 30, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(new JLabel("Emerging markets fund: veiex"), spanelgbc);

		spanelgbc.gridy = 3;
		spanelgbc.gridx = 1;
		spanelgbc.insets = new Insets(5, 70, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(this.stock3, spanelgbc);

		spanelgbc.gridy = 3;
		spanelgbc.gridx = 2;
		spanelgbc.insets = new Insets(5, -10, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(this.showstock3, spanelgbc);

		spanelgbc.gridy = 3;
		spanelgbc.gridx = 3;
		spanelgbc.insets = new Insets(5, -10, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(this.showccstock3, spanelgbc);

		spanelgbc.gridy = 4;
		spanelgbc.gridx = 0;
		spanelgbc.insets = new Insets(5, 30, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(new JLabel("Long-term bond fund: vbltx"), spanelgbc);

		spanelgbc.gridy = 4;
		spanelgbc.gridx = 1;
		spanelgbc.insets = new Insets(5, 70, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(this.stock4, spanelgbc);

		spanelgbc.gridy = 4;
		spanelgbc.gridx = 2;
		spanelgbc.insets = new Insets(5, -10, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(this.showstock4, spanelgbc);

		spanelgbc.gridy = 4;
		spanelgbc.gridx = 3;
		spanelgbc.insets = new Insets(5, -10, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(this.showccstock4, spanelgbc);

		spanelgbc.gridy = 5;
		spanelgbc.gridx = 0;
		spanelgbc.insets = new Insets(5, 30, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(new JLabel("Short-term bond fund: vbisx"), spanelgbc);

		spanelgbc.gridy = 5;
		spanelgbc.gridx = 1;
		spanelgbc.insets = new Insets(5, 70, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(this.stock5, spanelgbc);

		spanelgbc.gridy = 5;
		spanelgbc.gridx = 2;
		spanelgbc.insets = new Insets(5, -10, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(this.showstock5, spanelgbc);

		spanelgbc.gridy = 5;
		spanelgbc.gridx = 3;
		spanelgbc.insets = new Insets(5, -10, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(this.showccstock5, spanelgbc);

		spanelgbc.gridy = 6;
		spanelgbc.gridx = 0;
		spanelgbc.insets = new Insets(5, 30, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(new JLabel("Pacific stock index: vpacx"), spanelgbc);

		spanelgbc.gridy = 6;
		spanelgbc.gridx = 1;
		spanelgbc.insets = new Insets(5, 70, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(this.stock6, spanelgbc);

		spanelgbc.gridy = 6;
		spanelgbc.gridx = 2;
		spanelgbc.insets = new Insets(5, -10, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(this.showstock6, spanelgbc);

		spanelgbc.gridy = 6;
		spanelgbc.gridx = 3;
		spanelgbc.insets = new Insets(5, -10, 5, 10);
		spanelgbc.gridwidth = 1;
		stockPane.add(this.showccstock6, spanelgbc);

		// Items for Jpanel portsumpanel
		portsumgbc.anchor = GridBagConstraints.EAST;
		portsumgbc.gridy = 0;
		portsumgbc.gridx = 0;
		portsumgbc.insets = new Insets(5, 10, 5, 10);
		portsumgbc.gridwidth = 1;
		portsumpanel.add(new JLabel("Mean: "), portsumgbc);

		portsumgbc.gridy = 0;
		portsumgbc.gridx = 1;
		portsumgbc.insets = new Insets(5, -10, 5, 10);
		portsumgbc.gridwidth = 1;
		portsumpanel.add(promean, portsumgbc);

		// portsumgbc.gridy = 0;
		// portsumgbc.gridx = 2;
		// portsumgbc.insets = new Insets(5, 10, 5, 10);
		// portsumgbc.gridwidth = 1;
		// portsumpanel.add(new JLabel("Initial Wealth(x $10000):"),
		// portsumgbc);
		//
		// portsumgbc.gridy = 0;
		// portsumgbc.gridx = 3;
		// portsumgbc.insets = new Insets(5, 10, 5, 10);
		// portsumgbc.gridwidth = 1;
		// portsumpanel.add(initwealth, portsumgbc);

		portsumgbc.gridy = 1;
		portsumgbc.gridx = 0;
		portsumgbc.insets = new Insets(5, 10, 5, 10);
		portsumgbc.gridwidth = 1;
		portsumpanel.add(new JLabel("Variance: "), portsumgbc);

		portsumgbc.gridy = 1;
		portsumgbc.gridx = 1;
		portsumgbc.insets = new Insets(5, -10, 5, 10);
		portsumgbc.gridwidth = 1;
		portsumpanel.add(provariance, portsumgbc);

		// portsumgbc.gridy = 1;
		// portsumgbc.gridx = 2;
		// portsumgbc.insets = new Insets(5, 10, 5, 10);
		// portsumgbc.gridwidth = 1;
		// portsumpanel.add(new JLabel("Quantile"), portsumgbc);
		//
		// portsumgbc.gridy = 1;
		// portsumgbc.gridx = 3;
		// portsumgbc.insets = new Insets(5, 10, 5, 10);
		// portsumgbc.gridwidth = 1;
		// portsumpanel.add(proquantile, portsumgbc);

		portsumgbc.gridy = 2;
		portsumgbc.gridx = 0;
		portsumgbc.insets = new Insets(5, 10, 5, 10);
		portsumgbc.gridwidth = 1;
		portsumpanel.add(new JLabel("Standard Deivation: "), portsumgbc);

		portsumgbc.gridy = 2;
		portsumgbc.gridx = 1;
		portsumgbc.insets = new Insets(5, -10, 5, 10);
		portsumgbc.gridwidth = 1;
		portsumpanel.add(prosd, portsumgbc);

		portsumgbc.gridy = 3;
		portsumgbc.gridx = 0;
		portsumgbc.insets = new Insets(5, 10, 5, 10);
		portsumgbc.gridwidth = 1;
		portsumpanel.add(new JLabel("Value at Risk: "), portsumgbc);

		portsumgbc.gridy = 3;
		portsumgbc.gridx = 1;
		portsumgbc.insets = new Insets(5, -10, 5, 10);
		portsumgbc.gridwidth = 1;
		portsumpanel.add(provarisk, portsumgbc);

		portsumgbc.gridy = 4;
		portsumgbc.gridx = 0;
		portsumgbc.insets = new Insets(5, 10, 5, 10);
		portsumgbc.gridwidth = 1;
		portsumpanel.add(new JLabel("Sharpe Ratio:"), portsumgbc);

		portsumgbc.gridy = 4;
		portsumgbc.gridx = 1;
		portsumgbc.insets = new Insets(5, -10, 5, 10);
		portsumgbc.gridwidth = 1;
		portsumpanel.add(prosharpe, portsumgbc);
		//
		// portsumgbc.gridy = 5;
		// portsumgbc.gridx = 0;
		// portsumgbc.insets = new Insets(5, 10, 5, 10);
		// portsumgbc.gridwidth = 4;
		// portsumpanel.add(getportresult, portsumgbc);

		// Add elements to JPanel for asset allocation
		assetpanelgbc.gridy = 0;
		assetpanelgbc.gridx = 0;
		assetpanelgbc.insets = new Insets(0, -70, 5, 10);
		assetpanelgbc.gridwidth = 1;
		assetalloc.add(new JLabel("Asset A: "), assetpanelgbc);

		assetpanelgbc.gridy = 0;
		assetpanelgbc.gridx = 1;
		assetpanelgbc.insets = new Insets(0, 150, 5, 10);
		assetpanelgbc.gridwidth = 1;
		assetalloc.add(this.selectstockA, assetpanelgbc);

		assetpanelgbc.gridy = 1;
		assetpanelgbc.gridx = 0;
		assetpanelgbc.insets = new Insets(5, -70, 5, 10);
		assetpanelgbc.gridwidth = 1;
		assetalloc.add(new JLabel("Asset B: "), assetpanelgbc);

		assetpanelgbc.gridy = 1;
		assetpanelgbc.gridx = 1;
		assetpanelgbc.insets = new Insets(5, 150, 5, 10);
		assetpanelgbc.gridwidth = 1;
		assetalloc.add(this.selectstockB, assetpanelgbc);

		assetpanelgbc.gridy = 2;
		assetpanelgbc.gridx = 0;
		assetpanelgbc.insets = new Insets(5, 10, 5, 10);
		assetpanelgbc.gridwidth = 1;
		assetalloc.add(new JLabel("Allocation to Asset A: "), assetpanelgbc);

		assetpanelgbc.gridy = 2;
		assetpanelgbc.gridx = 1;
		assetpanelgbc.insets = new Insets(5, 150, 5, 10);
		assetpanelgbc.gridwidth = 1;
		assetalloc.add(this.stockA, assetpanelgbc);

		assetpanelgbc.gridy = 2;
		assetpanelgbc.gridx = 2;
		assetpanelgbc.insets = new Insets(5, -130, 5, 10);
		assetpanelgbc.gridwidth = 1;
		assetalloc.add(new JLabel("%"), assetpanelgbc);

		assetpanelgbc.gridy = 3;
		assetpanelgbc.gridx = 0;
		assetpanelgbc.insets = new Insets(5, 10, 5, 10);
		assetpanelgbc.gridwidth = 1;
		assetalloc.add(new JLabel("Allocation to Asset B: "), assetpanelgbc);

		assetpanelgbc.gridy = 3;
		assetpanelgbc.gridx = 1;
		assetpanelgbc.insets = new Insets(5, 150, 5, 10);
		assetpanelgbc.gridwidth = 1;
		assetalloc.add(this.stockB, assetpanelgbc);

		assetpanelgbc.gridy = 3;
		assetpanelgbc.gridx = 2;
		assetpanelgbc.insets = new Insets(5, -130, 5, 10);
		assetpanelgbc.gridwidth = 1;
		assetalloc.add(new JLabel("%"), assetpanelgbc);

		assetpanelgbc.gridy = 4;
		assetpanelgbc.gridx = 0;
		assetpanelgbc.insets = new Insets(5, 25, 5, 10);
		assetpanelgbc.gridwidth = 1;
		assetalloc.add(new JLabel("Initial Wealth(x$10,000):"), assetpanelgbc);

		assetpanelgbc.gridy = 4;
		assetpanelgbc.gridx = 1;
		assetpanelgbc.insets = new Insets(5, 150, 5, 10);
		assetpanelgbc.gridwidth = 1;
		assetalloc.add(initwealth, assetpanelgbc);

		assetpanelgbc.gridy = 5;
		assetpanelgbc.gridx = 0;
		assetpanelgbc.insets = new Insets(5, 15, 5, 10);
		assetpanelgbc.gridwidth = 1;
		assetalloc.add(new JLabel("Quantile (Default: 0.05):"), assetpanelgbc);

		assetpanelgbc.gridy = 5;
		assetpanelgbc.gridx = 1;
		assetpanelgbc.insets = new Insets(5, 150, 5, 10);
		assetpanelgbc.gridwidth = 1;
		assetalloc.add(proquantile, assetpanelgbc);

		// assetpanelgbc.gridy = 6;
		// assetpanelgbc.gridx = 0;
		// assetpanelgbc.insets = new Insets(5, 15, 5, 10);
		// assetpanelgbc.gridwidth = 1;
		// assetalloc.add(new JLabel("Expected Return:"), assetpanelgbc);
		//
		// assetpanelgbc.gridy = 6;
		// assetpanelgbc.gridx = 1;
		// assetpanelgbc.insets = new Insets(5, 150, 5, 10);
		// assetpanelgbc.gridwidth = 1;
		// assetalloc.add(exprtn, assetpanelgbc);
		//
		// assetpanelgbc.gridy = 7;
		// assetpanelgbc.gridx = 0;
		// assetpanelgbc.insets = new Insets(5, 15, 5, 10);
		// assetpanelgbc.gridwidth = 1;
		// assetalloc.add(new JLabel("Expected Risk:"), assetpanelgbc);
		//
		// assetpanelgbc.gridy = 7;
		// assetpanelgbc.gridx = 1;
		// assetpanelgbc.insets = new Insets(5, 150, 5, 10);
		// assetpanelgbc.gridwidth = 1;
		// assetalloc.add(exprisk, assetpanelgbc);

		assetpanelgbc.gridy = 8;
		assetpanelgbc.gridx = 1;
		assetpanelgbc.insets = new Insets(5, -250, 5, 10);
		assetpanelgbc.gridwidth = 1;
		assetalloc.add(updateassetalloc, assetpanelgbc);

		assetpanelgbc.gridy = 8;
		assetpanelgbc.gridx = 2;
		assetpanelgbc.insets = new Insets(5, -380, 5, 10);
		assetpanelgbc.gridwidth = 1;
		assetalloc.add(this.reportassetalloc, assetpanelgbc);

		// Definition of effport subpanel
		efpanelgbc.anchor = GridBagConstraints.WEST;
		efpanelgbc.gridy = 0;
		efpanelgbc.gridx = 0;
		efpanelgbc.insets = new Insets(5, 0, 5, 10);
		efpanelgbc.gridwidth = 2;
		effport.add(new JLabel("Global Minimum Variance Portfolio"), efpanelgbc);

		efpanelgbc.gridy = 0;
		efpanelgbc.gridx = 2;
		efpanelgbc.insets = new Insets(5, 10, 5, 10);
		efpanelgbc.gridwidth = 1;
		effport.add(genglobalminpro, efpanelgbc);

		efpanelgbc.gridy = 1;
		efpanelgbc.gridx = 0;
		efpanelgbc.insets = new Insets(5, 0, 5, 10);
		efpanelgbc.gridwidth = 2;
		effport.add(new JLabel("Tangency Portfolio"), efpanelgbc);

		efpanelgbc.gridy = 1;
		efpanelgbc.gridx = 2;
		efpanelgbc.insets = new Insets(5, 10, 5, 10);
		efpanelgbc.gridwidth = 1;
		effport.add(gentanpro, efpanelgbc);

		efpanelgbc.gridy = 3;
		efpanelgbc.gridx = 0;
		efpanelgbc.insets = new Insets(5, 0, 5, 10);
		efpanelgbc.gridwidth = 2;
		effport.add(new JLabel("Expected Return (Ex: 0.008)"), efpanelgbc);

		efpanelgbc.gridy = 3;
		efpanelgbc.gridx = 1;
		efpanelgbc.insets = new Insets(5, 200, 5, 10);
		efpanelgbc.gridwidth = 1;
		effport.add(porttgtexp, efpanelgbc);

		efpanelgbc.gridy = 3;
		efpanelgbc.gridx = 2;
		efpanelgbc.insets = new Insets(5, 10, 5, 10);
		efpanelgbc.gridwidth = 1;
		effport.add(gentanexprtn, efpanelgbc);

		efpanelgbc.gridy = 4;
		efpanelgbc.gridx = 0;
		efpanelgbc.insets = new Insets(5, 0, 5, 10);
		efpanelgbc.gridwidth = 2;
		effport.add(new JLabel("Expected Risk (Ex: 0.06)"), efpanelgbc);

		efpanelgbc.gridy = 4;
		efpanelgbc.gridx = 1;
		efpanelgbc.insets = new Insets(5, 200, 5, 10);
		efpanelgbc.gridwidth = 1;
		effport.add(porttgtrisk, efpanelgbc);

		efpanelgbc.gridy = 4;
		efpanelgbc.gridx = 2;
		efpanelgbc.insets = new Insets(5, 10, 5, 10);
		efpanelgbc.gridwidth = 1;
		effport.add(gentgtriskrtn, efpanelgbc);

		secleftpanel.add(stockPane);
		secleftpanel.add(portsumpanel);

		secrightpanel.add(assetalloc);
		secrightpanel.add(effport);

		secmainpanel.add(secleftpanel);
		secmainpanel.add(secrightpanel);

		/* tpanelgbc.gridy = 2; tpanelgbc.insets = new Insets(10, 300, 10, 10); tpanelgbc.gridwidth = 1; contentPane.add(new JLabel( "Final Result: (Either Enter value in Asset Allocation and Users expectations)" ),tpanelgbc); tpanelgbc.gridy = 3; tpanelgbc.insets = new Insets(10, 0, 10, 10); tpanelgbc.gridwidth = 3; contentPane.add(new JLabel(
		 * "======================================= Asset Allocation Part =======================================" ),tpanelgbc); tpanelgbc.gridy = 4; tpanelgbc.insets = new Insets(10, -500, 10, 10); tpanelgbc.gridwidth = 1; contentPane.add(new JLabel("Asset Allocation: Stock A"),tpanelgbc); tpanelgbc.gridx = 1; tpanelgbc.insets = new Insets(10, 50, 10, 10); contentPane.add(this.stockA,tpanelgbc);
		 * tpanelgbc.gridx = 2; tpanelgbc.insets = new Insets(10, 0, 10, 10); contentPane.add(new JLabel("%"),tpanelgbc); tpanelgbc.gridy = 5; tpanelgbc.gridx = 0; tpanelgbc.insets = new Insets(10, -500, 10, 10); tpanelgbc.gridwidth = 1; contentPane.add(new JLabel("Asset Allocation: Stock B"),tpanelgbc); tpanelgbc.gridx = 1; tpanelgbc.insets = new Insets(10, 50, 10, 10);
		 * contentPane.add(this.stockB,tpanelgbc); tpanelgbc.gridx = 2; tpanelgbc.insets = new Insets(10, 0, 10, 10); contentPane.add(new JLabel("%"),tpanelgbc); tpanelgbc.gridy = 6; tpanelgbc.gridx = 0; tpanelgbc.insets = new Insets(10, 0, 10, 10); tpanelgbc.gridwidth = 3; contentPane.add(new JLabel(
		 * "======================================== Users expectations =========================================" ),tpanelgbc); tpanelgbc.gridy = 7; tpanelgbc.gridx = 0; tpanelgbc.insets = new Insets(10, -10, 10, 10); tpanelgbc.gridwidth = 1; contentPane.add(new JLabel("User expectations - Expected Value: "),tpanelgbc); tpanelgbc.gridx = 1; tpanelgbc.insets = new Insets(10, 50, 10, 10);
		 * contentPane.add(this.proexp,tpanelgbc); tpanelgbc.gridy = 8; tpanelgbc.gridx = 0; tpanelgbc.insets = new Insets(10, -420, 10, 10); tpanelgbc.gridwidth = 1; contentPane.add(new JLabel("User expectations - Standard Deviation: "),tpanelgbc); tpanelgbc.gridx = 1; tpanelgbc.insets = new Insets(10, 50, 10, 10); contentPane.add(this.prosd,tpanelgbc); tpanelgbc.gridy = 9; tpanelgbc.gridx = 0;
		 * tpanelgbc.insets = new Insets(10, -480, 10, 10); tpanelgbc.gridwidth = 1; contentPane.add(new JLabel("User expectations - Variance: "),tpanelgbc); tpanelgbc.gridx = 1; tpanelgbc.insets = new Insets(10, 50, 10, 10); contentPane.add(this.provar,tpanelgbc); tpanelgbc.gridy = 10; tpanelgbc.gridx = 0; tpanelgbc.gridwidth = 3; contentPane.add(sp2,tpanelgbc); */

		buttongbc.gridx = 0;
		buttongbc.gridy = 0;
		buttonPane.add(this.startApp, buttongbc);

		buttongbc.gridx = 1;
		buttonPane.add(this.exitApp, buttongbc);

		tabbedpane.addTab("Portfolio Analysis and Assets Allocation", secmainpanel);
		tabbedpane.setPreferredSize(new Dimension(900, 900));

		getContentPane().add(contentPane, BorderLayout.PAGE_START);
		getContentPane().add(tabbedpane, BorderLayout.CENTER);
		// getContentPane().add(buttonPane, BorderLayout.PAGE_END);

	}

	public void addStockdata(String stockname) {
		int numberOfrows = this.tableModel.getRowCount();

		if (numberOfrows > 0) {
			System.out.println("Row: " + numberOfrows);
			for (int i = 0; i < numberOfrows; i++) {
				System.out.println("Remove");
				this.tableModel.removeRow(0);
			}
		}

		if (stockname.equals("all")) {
			this.tableModel.addRow(buildtabledata("vfinx"));
			this.tableModel.addRow(buildtabledata("veurx"));
			this.tableModel.addRow(buildtabledata("veiex"));
			this.tableModel.addRow(buildtabledata("vbltx"));
			this.tableModel.addRow(buildtabledata("vbisx"));
			this.tableModel.addRow(buildtabledata("vpacx"));
			this.tableModel.addRow(buildtabledata("goog"));
			this.tableModel.addRow(buildtabledata("msft"));
		} else {
			this.tableModel.addRow(buildtabledata(stockname));
		}

	}

	public Object[] buildtabledata(String stockname) {
		FinancialFunctionCaller buildstockdata = new FinancialFunctionCaller(stockname);
		buildstockdata.run();

		return new Object[] { stockname, buildstockdata.fcmean, buildstockdata.fcsd, buildstockdata.fcvariance, buildstockdata.fcskew, buildstockdata.fckurt, buildstockdata.fcvar, buildstockdata.fcsharpe };
	}

	public void logdisplay() {
		for (int i = 0; i < this.logger.size(); i++) {
			System.out.println(this.logger.get(i));
		}
	}

	public void showGraphic(String graphic_name, String type) {

		// Initial Graph of Continuing Compounding Return: When the second
		// parameter set to "cc", the graph will show the Continuing Compounding
		// Graph
		GraphGenerator stockPriceGraph = new GraphGenerator(graphic_name, type);
		stockPriceGraph.pack();
		RefineryUtilities.centerFrameOnScreen(stockPriceGraph);
		stockPriceGraph.setVisible(true);
		stockPriceGraph.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(stock1)) {
			if (this.stock1.isSelected()) {
				if (this.selectedStock < 2) {
					this.selectedStock++;
					if (this.stockcombination.size() == 0) {
						this.selectstockA.setText("vfinx");
					} else {
						this.selectstockB.setText("vfinx");
					}
					this.stockcombination.add("vfinx");
				} else {
					JOptionPane.showMessageDialog(this, "Please choose 2 stocks only");
					this.stock1.setSelected(false);
				}
			} else {
				System.out.println("Index of vfinx: " + this.stockcombination.indexOf("vfinx"));
				if (this.stockcombination.indexOf("vfinx") == 0) {
					if (this.stockcombination.size() > 1) {
						this.selectstockA.setText(this.stockcombination.get(1));
						this.selectstockB.setText("Not Selected");
					} else {
						this.selectstockA.setText("Not Selected");
					}
				} else {
					this.selectstockB.setText("Not Selected");
				}
				this.stockcombination.remove(this.stockcombination.indexOf("vfinx"));
				this.selectedStock--;
			}
			System.out.println("Checked: " + this.selectedStock);
		} else if (e.getSource().equals(stock2)) {
			if (this.stock2.isSelected()) {
				if (this.selectedStock < 2) {
					this.selectedStock++;
					if (this.stockcombination.size() == 0) {
						this.selectstockA.setText("veurx");
					} else {
						this.selectstockB.setText("veurx");
					}
					this.stockcombination.add("veurx");
				} else {
					JOptionPane.showMessageDialog(this, "Please choose 2 stocks only");
					this.stock2.setSelected(false);
				}
			} else {
				this.selectedStock--;
				if (this.stockcombination.indexOf("veurx") == 0) {
					if (this.stockcombination.size() > 1) {
						this.selectstockA.setText(this.stockcombination.get(1));
						this.selectstockB.setText("Not Selected");
					} else {
						this.selectstockA.setText("Not Selected");
					}
				} else {
					this.selectstockB.setText("Not Selected");
				}
				System.out.println("Index of veurx: " + this.stockcombination.indexOf("veurx"));
				this.stockcombination.remove(this.stockcombination.indexOf("veurx"));
			}
			System.out.println("Checked: " + this.selectedStock);
		} else if (e.getSource().equals(stock3)) {
			if (this.stock3.isSelected()) {
				if (this.selectedStock < 2) {
					this.selectedStock++;
					if (this.stockcombination.size() == 0) {
						this.selectstockA.setText("veiex");
					} else {
						this.selectstockB.setText("veiex");
					}
					this.stockcombination.add("veiex");
				} else {
					JOptionPane.showMessageDialog(this, "Please choose 2 stocks only");
					this.stock3.setSelected(false);
				}
			} else {
				this.selectedStock--;
				if (this.stockcombination.indexOf("veiex") == 0) {
					if (this.stockcombination.size() > 1) {
						this.selectstockA.setText(this.stockcombination.get(1));
						this.selectstockB.setText("Not Selected");
					} else {
						this.selectstockA.setText("Not Selected");
					}
				} else {
					this.selectstockB.setText("Not Selected");
				}
				System.out.println("Index of veiex: " + this.stockcombination.indexOf("veiex"));
				this.stockcombination.remove(this.stockcombination.indexOf("veiex"));
			}
			System.out.println("Checked: " + this.selectedStock);
		} else if (e.getSource().equals(stock4)) {
			if (this.stock4.isSelected()) {
				if (this.selectedStock < 2) {
					if (this.stockcombination.size() == 0) {
						this.selectstockA.setText("vbltx");
					} else {
						this.selectstockB.setText("vbltx");
					}
					this.selectedStock++;
					this.stockcombination.add("vbltx");
				} else {
					JOptionPane.showMessageDialog(this, "Please choose 2 stocks only");
					this.stock4.setSelected(false);
				}
			} else {
				this.selectedStock--;
				if (this.stockcombination.indexOf("vbltx") == 0) {
					if (this.stockcombination.size() > 1) {
						this.selectstockA.setText(this.stockcombination.get(1));
						this.selectstockB.setText("Not Selected");
					} else {
						this.selectstockA.setText("Not Selected");
					}
				} else {
					this.selectstockB.setText("Not Selected");
				}
				System.out.println("Index of vbltx: " + this.stockcombination.indexOf("vbltx"));
				this.stockcombination.remove(this.stockcombination.indexOf("vbltx"));
			}
			System.out.println("Checked: " + this.selectedStock);
		} else if (e.getSource().equals(stock5)) {
			if (this.stock5.isSelected()) {
				if (this.selectedStock < 2) {
					if (this.stockcombination.size() == 0) {
						this.selectstockA.setText("vbisx");
					} else {
						this.selectstockB.setText("vbisx");
					}
					this.selectedStock++;
					this.stockcombination.add("vbisx");
				} else {
					JOptionPane.showMessageDialog(this, "Please choose 2 stocks only");
					this.stock5.setSelected(false);
				}
			} else {
				this.selectedStock--;
				if (this.stockcombination.indexOf("vbisx") == 0) {
					if (this.stockcombination.size() > 1) {
						this.selectstockA.setText(this.stockcombination.get(1));
						this.selectstockB.setText("Not Selected");
					} else {
						this.selectstockA.setText("Not Selected");
					}
				} else {
					this.selectstockB.setText("Not Selected");
				}
				System.out.println("Index of vbisx: " + this.stockcombination.indexOf("vbisx"));
				this.stockcombination.remove(this.stockcombination.indexOf("vbisx"));
			}
			System.out.println("Checked: " + this.selectedStock);
		} else if (e.getSource().equals(stock6)) {
			if (this.stock6.isSelected()) {
				if (this.selectedStock < 2) {
					if (this.stockcombination.size() == 0) {
						this.selectstockA.setText("vpacx");
					} else {
						this.selectstockB.setText("vpacx");
					}
					this.selectedStock++;
					this.stockcombination.add("vpacx");
				} else {
					JOptionPane.showMessageDialog(this, "Please choose 2 stocks only");
					this.stock6.setSelected(false);
				}
			} else {
				this.selectedStock--;
				if (this.stockcombination.indexOf("vpacx") == 0) {
					if (this.stockcombination.size() > 1) {
						this.selectstockA.setText(this.stockcombination.get(1));
						this.selectstockB.setText("Not Selected");
					} else {
						this.selectstockA.setText("Not Selected");
					}
				} else {
					this.selectstockB.setText("Not Selected");
				}
				System.out.println("Index of vpacx: " + this.stockcombination.indexOf("vpacx"));
				this.stockcombination.remove(this.stockcombination.indexOf("vpacx"));
			}
			System.out.println("Checked: 6 " + this.selectedStock);
		} else { // No action needed
		}

		if (e.getSource().equals(showstock1)) {
			showGraphic("vfinx", "price");
		}

		if (e.getSource().equals(showstock2)) {
			showGraphic("veurx", "price");
		}

		if (e.getSource().equals(showstock3)) {
			showGraphic("veiex", "price");
		}

		if (e.getSource().equals(showstock4)) {
			showGraphic("vbltx", "price");
		}

		if (e.getSource().equals(showstock5)) {
			showGraphic("vbisx", "price");
		}

		if (e.getSource().equals(showstock6)) {
			showGraphic("vpacx", "price");
		}

		if (e.getSource().equals(showccstock1)) {
			showGraphic("vfinx", "cc");
		}

		if (e.getSource().equals(showccstock2)) {
			showGraphic("veurx", "cc");
		}

		if (e.getSource().equals(showccstock3)) {
			showGraphic("veiex", "cc");
		}

		if (e.getSource().equals(showccstock4)) {
			showGraphic("vbltx", "cc");
		}

		if (e.getSource().equals(showccstock5)) {
			showGraphic("vbisx", "cc");
		}

		if (e.getSource().equals(showccstock6)) {
			showGraphic("vpacx", "cc");
		}

		if (e.getActionCommand().equals("Start")) {

		}

		if (e.getActionCommand().equals("Show data")) {
			if (!this.singlestock.getText().equals("")) {
				addStockdata(this.singlestock.getText());
			} else {
				JOptionPane.showMessageDialog(this, "Invaild Stocks Code!!!");
			}
		}

		if (e.getActionCommand().equals("Show All")) {
			addStockdata("all");
		}

		if (e.getSource().equals(updateassetalloc)) {
			try {

				double initwealth = 1.0;
				double initquan = 0.05;
				double initexprtn = 0.008;
				double initexprisk = 0.06;

				// if (!(this.exprtn.getText().equals(""))) {
				// initexprtn = Double.parseDouble(this.exprtn.getText());
				// } else {
				// this.exprtn.setText(Double.toString(initexprtn));
				// }
				//
				// if (!(this.exprisk.getText().equals(""))) {
				// initexprisk = Double.parseDouble(this.exprisk.getText());
				// } else {
				// this.exprisk.setText(Double.toString(initexprisk));
				// }

				if (!(this.initwealth.getText().equals(""))) {
					initwealth = Double.parseDouble(this.initwealth.getText());
				} else {
					this.initwealth.setText(Double.toString(initwealth));
				}

				if (!(this.proquantile.getText().equals(""))) {
					initquan = Double.parseDouble(this.proquantile.getText());
				} else {
					this.proquantile.setText(Double.toString(initquan));
				}
				double stockAshare = Double.parseDouble(this.stockA.getText());
				double stockBshare = Double.parseDouble(this.stockB.getText());
				if (Double.parseDouble(this.stockA.getText()) > 0) {
					if (Double.parseDouble(this.stockB.getText()) <= 0) {
						// Do nothing
						// stockAshare = stockAshare;

					} else {
						stockAshare = (Double.parseDouble(this.stockA.getText()));
					}
				} else {
					stockAshare = Double.parseDouble(this.stockA.getText());
				}

				if (Double.parseDouble(this.stockB.getText()) > 0) {
					if (Double.parseDouble(this.stockA.getText()) <= 0) {
						// stockBshare = stockBshare;
					} else {
						stockBshare = (Double.parseDouble(this.stockB.getText()));
					}
				} else {
					stockBshare = Double.parseDouble(this.stockB.getText());
				}

				this.stockA.setText(Double.toString(stockAshare));
				this.stockB.setText(Double.toString(stockBshare));
				this.allocatedstockA = stockAshare;
				this.allocatedstockB = stockBshare;
				this.allocatedinitwealth = initwealth;
				this.allocatedinitquan = initquan;
				// this.allocatedexprtn = initexprtn;
				// this.allocatedexprisk = initexprisk;
			} catch (NumberFormatException nfe) {
				JOptionPane.showMessageDialog(this, "Allocation of stock A and B must be a number");
			}
			System.out.println("Stock A: " + this.allocatedstockA + " Stock B: " + this.allocatedstockB);

			if ((this.allocatedstockA + this.allocatedstockB) != 1) {
				JOptionPane.showMessageDialog(this, "Allocation of Asset A and Asset B must be equal to 1");
			} else {
				this.alloccontrolbit = 'T';
			}

			if (this.stockcombination.size() < 2) {
				JOptionPane.showMessageDialog(this, "Please choose 2 stocks for next step");
			} else {
				System.out.println("Selected Stock: " + this.stockcombination.get(0) + " and: " + this.stockcombination.get(1));
				this.stockcontrolbit = 'T';
			}

			if (this.stockcontrolbit == 'T' && this.alloccontrolbit == 'T') {
				System.out.println("Debug: " + this.allocatedinitwealth + " Quan: " + this.allocatedinitquan);
				this.logger.add("Time: " + this.dateAsText.format(new Date()) + " Begin Portfilio Calucation");
				this.logger.add("Asset A: " + this.stockcombination.get(0) + " Asset B: " + this.stockcombination.get(1));
				this.logger.add("Allocation of A: " + this.allocatedstockA + " Allocation of B: " + this.allocatedstockB);
				this.logger.add("Invest price: " + (this.allocatedinitwealth * 10000) + " Quantile: " + this.allocatedinitquan);
				this.logger.add("Expected Return: " + this.allocatedexprtn + " Expected Risk: " + this.allocatedexprisk);

				FinancialFunctionCaller showportfiliosummay = new FinancialFunctionCaller(this.stockcombination.get(0), this.stockcombination.get(1), this.allocatedstockA, this.allocatedstockB, this.allocatedinitwealth, this.allocatedinitquan);
				showportfiliosummay.run();

				System.out.println("debug: " + showportfiliosummay.pmean.toString());

				this.promean.setText(showportfiliosummay.pmean.toString());
				this.provariance.setText(showportfiliosummay.pvariance.toString());
				this.prosd.setText(showportfiliosummay.psd.toString());
				this.provarisk.setText(showportfiliosummay.pvar.toString());
				this.prosharpe.setText(showportfiliosummay.psharpe.toString());

				this.logger.add("================= Result: ====================");
				this.logger.add("Portfilio Mean: " + showportfiliosummay.pmean.toString());
				this.logger.add("Portfilio Variance: " + showportfiliosummay.pvariance.toString());
				this.logger.add("Portfilio Standard Deviation: " + showportfiliosummay.psd.toString());
				this.logger.add("Portfilio VaR: " + showportfiliosummay.pvar.toString());
				this.logger.add("Portfilio Sharpe Ratio: " + showportfiliosummay.psharpe.toString());
				this.logger.add("==============================================");
				showportfiliosummay = null;
				logdisplay();
			}
		}

		if (e.getSource().equals(genglobalminpro)) {
			if (this.stockcombination.size() < 2) {
				JOptionPane.showMessageDialog(this, "Please choose 2 stocks for next step");
			} else {
				System.out.println("Selected Stock: " + this.stockcombination.get(0) + " and: " + this.stockcombination.get(1));
				this.stockcontrolbit = 'T';

				FinancialFunctionCaller showportfiliosummay = new FinancialFunctionCaller(this.stockcombination.get(0), this.stockcombination.get(1), 0.5, 0.5);
				showportfiliosummay.run();
				this.logger.add("Time: " + this.dateAsText.format(new Date()) + "Being to Calculate Golbal Minimum Variance Portfolio:");
				this.logger.add("================= Result: ====================");
				this.logger.add("Set Allocation of Asset A to: " + showportfiliosummay.pgminA.toString());
				this.logger.add("Set Allocation of Asset B to: " + showportfiliosummay.pgminB.toString());
				this.stockA.setText(showportfiliosummay.pgminA.toString());
				this.stockB.setText(showportfiliosummay.pgminB.toString());
				this.logger.add("==============================================");
			}
		}

		if (e.getSource().equals(gentanpro)) {
			if (this.stockcombination.size() < 2) {
				JOptionPane.showMessageDialog(this, "Please choose 2 stocks for next step");
			} else {
				System.out.println("Selected Stock: " + this.stockcombination.get(0) + " and: " + this.stockcombination.get(1));
				this.stockcontrolbit = 'T';

				FinancialFunctionCaller showportfiliosummay = new FinancialFunctionCaller(this.stockcombination.get(0), this.stockcombination.get(1), 0.5, 0.5);
				showportfiliosummay.run();
				this.stockA.setText(showportfiliosummay.ptanA.toString());
				this.stockB.setText(showportfiliosummay.ptanB.toString());
				this.logger.add("Time: " + this.dateAsText.format(new Date()) + "Being to Calculate Tangency Portfolio:");
				this.logger.add("================= Result: ====================");
				this.logger.add("Set Allocation of Asset A to: " + showportfiliosummay.ptanA.toString());
				this.logger.add("Set Allocation of Asset B to: " + showportfiliosummay.ptanB.toString());
				this.logger.add("==============================================");
			}
		}

		if (e.getSource().equals(gentanexprtn)) {
			if (this.stockcombination.size() < 2) {
				JOptionPane.showMessageDialog(this, "Please choose 2 stocks for next step");
			} else {
				System.out.println("Selected Stock: " + this.stockcombination.get(0) + " and: " + this.stockcombination.get(1));
				this.stockcontrolbit = 'T';

				try {
					double allocatedexprtn = -99999;

					if (!(this.porttgtexp.getText().equals(""))) {
						allocatedexprtn = Double.parseDouble(this.porttgtexp.getText());
					} else {
						JOptionPane.showMessageDialog(this, "You must enter a number to the field");
					}

					if (allocatedexprtn != -99999) {
						if (this.stockcombination.size() == 2) {
							double allcA = 0.5;
							double allcB = 0.5;
							double allcw = 0.1;
							double allcq = 0.05;
							this.logger.add("Time: " + this.dateAsText.format(new Date()) + " Being to calculate expected return");
							// System.out.println("Allocated exp rtn: "+allocatedexprtn+" Alloc A: "+this.allocatedstockA+
							// "Alloc B: "+this.allocatedstockB);
							FinancialFunctionCaller showportfiliosummay = new FinancialFunctionCaller(this.stockcombination.get(0), this.stockcombination.get(1), allcA, allcB, allcw, allcq, allocatedexprtn, 0.6);
							showportfiliosummay.run();
							this.stockA.setText(showportfiliosummay.peffRiskA.toString());
							this.stockB.setText(showportfiliosummay.peffRiskB.toString());
							this.porttgtrisk.setText(showportfiliosummay.peffRisk.toString());

							this.logger.add("=================== Result ===================");
							this.logger.add("Set Allocation of A: " + showportfiliosummay.peffReturnA.toString());
							this.logger.add("Set Allocation of B: " + showportfiliosummay.peffReturnB.toString());
							this.logger.add("Set Expected Return to: " + showportfiliosummay.peffRisk.toString());
							this.logger.add("==============================================");

						} else {
							JOptionPane.showMessageDialog(this, "You must select 2 stocks first");
						}
					}
				} catch (NumberFormatException nfe) {
					JOptionPane.showMessageDialog(this, "Expected Return must be a number");
				}
			}
		}

		// Targetted Risk
		if (e.getSource().equals(gentgtriskrtn)) {
			try {
				double allocatedexprisk = -99999; // Initial value for Target Expected Return, which shouldn't be -99999

				if (!(this.porttgtrisk.getText().equals(""))) {
					allocatedexprisk = Double.parseDouble(this.porttgtrisk.getText());
				} else {
					JOptionPane.showMessageDialog(this, "You must enter a number to the field");
				}

				if (allocatedexprisk != -99999) {
					if (this.stockcombination.size() == 2) {
						double allcA = 0.5;
						double allcB = 0.5;
						double allcw = 0.1;
						double allcq = 0.05;
						double allcrtn = 0.008;
						
						this.logger.add("Time: " + this.dateAsText.format(new Date()) + " Being to calculate expected risk");
						// System.out.println("Allocated exp rtn: "+allocatedexprtn+" Alloc A: "+this.allocatedstockA+
						// "Alloc B: "+this.allocatedstockB);
						FinancialFunctionCaller showportfiliosummay = new FinancialFunctionCaller(this.stockcombination.get(0), this.stockcombination.get(1), allcA, allcB, allcw, allcq, 0.008, allocatedexprisk);
						showportfiliosummay.run();
						this.stockA.setText(showportfiliosummay.peffReturnA.toString());
						this.stockB.setText(showportfiliosummay.peffReturnB.toString());
						this.porttgtexp.setText(showportfiliosummay.peffReturn.toString());

						this.logger.add("=================== Result ===================");
						this.logger.add("Set Allocation of A: " + showportfiliosummay.peffReturnA.toString());
						this.logger.add("Set Allocation of B: " + showportfiliosummay.peffReturnB.toString());
						this.logger.add("Set Expected Risk to: " + showportfiliosummay.peffReturn.toString());
						this.logger.add("==============================================");

					} else {
						JOptionPane.showMessageDialog(this, "You must select 2 stocks first");
					}
				}
			} catch (NumberFormatException nfe) {
				JOptionPane.showMessageDialog(this, "Expected Return must be a number");
			}
		}

		if (e.getSource().equals(reportassetalloc)) {
			this.stockA.setText("0.5");
			this.stockB.setText("0.5");
			this.proquantile.setText("0.05");
			this.initwealth.setText("1.0");
			//this.exprtn.setText("0.008");
			//this.exprisk.setText("0.6");
		}

		if (e.getSource().equals(saveresult)) {
			System.out.println("SAVE");
			if (this.logger.size() > 1) {
				JFileChooser filechooser = new JFileChooser();
				int selected = filechooser.showSaveDialog(this);
				if (selected == JFileChooser.APPROVE_OPTION) {
					try {
						File file = filechooser.getSelectedFile();
						System.out.println(file.getAbsolutePath());
						PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));

						for (int i = 0; i < this.logger.size(); i++) {
							pw.println(this.logger.get(i));
						}

						pw.close();

					} catch (IOException exp) {
						System.out.println("Can't write file");
					}

				}
			} else {
				JOptionPane.showMessageDialog(this, "You haven't do anything");
			}
		}

	}
}
